import axios from "axios";

//处理oj前台的请求
const ojApi = {
    //Home页的请求
    getWebsiteConfig() {
        return ajax('/api/get-website-config', 'get', {})
    },
    getHomeCarousel() {
        return ajax('/api/home-carousel', 'get', {})
    },

    //用户账户的相关请求
    getRegisterEmail(email) {
        let params = {
            email: email
        }
        return ajax('/api/get-register-code', 'get', {
            params
        })
    },
    checkUsernameOrEmail(username, email) {
        return ajax('/api/check-username-or-email', 'post', {
            data: {
                username,
                email
            }
        })
    },
    getCaptcha() {
        return ajax('/api/captcha', 'get')
    },
    login(data) {
        return ajax('/api/login', 'post', {
            data
        })
    },
    register(data) {
        return ajax('/api/register', 'post', {
            data
        })
    },
    logout() {
        return ajax('/api/logout', 'get')
    },

    //账户的相关操作
    applyResetPassword(data) {
        return ajax('/api/apply-reset-password', 'post', {
            data
        })
    },
    resetPassword(data) {
        return ajax('/api/reset-password', 'post', {
            data
        })
    },


    //about页部分请求
    getAllLanguages() {
        return ajax("/api/languages", 'get')
    },

    //setting页的请求
    changePassword(data) {
        return ajax("/api/change-password", 'post', {
            data
        })
    },
    changeEmail(data) {
        return ajax("/api/change-email", 'post', {
            data
        })
    },
    changeUserInfo(data) {
        return ajax("/api/change-userInfo", 'post', {
            data
        })
    },

    //站内消息相关请求
    getUnreadMsgCount() {
        return ajax("/api/msg/unread", 'get')
    },

    //讨论相关
    getDiscussionList(limit, searchParams) {
        let params = {
            limit
        }
        Object.keys(searchParams).forEach((element) => {
            if (searchParams[element] !== '' && searchParams[element] !== null && searchParams[element] !== undefined) {
                params[element] = searchParams[element]
            }
        })
        return ajax("/api/get-discussion-list", 'get', {
            params
        })
    },

    //获取题目标签和分类
    getProblemTagsAndClassification() {
        return ajax('/api/get-problem-tags-and-classification', 'get')
    },
    //获取题目代码模板
    getProblemCodeTemplate(pid) {
        return ajax('/api/get-problem-code-template', 'get', {
            params: {
                pid
            }
        })
    },
    //获取所有支持的题目语言
    getLanguages() {
        return ajax('/api/languages', 'get')
    },
    //获取题目支持的题目语言
    getProblemLanguages(pid) {
        return ajax('/api/get-problem-languages', 'get', {
            params: {
                pid: pid
            }
        })
    },


}

export default ojApi

/**
 * @param url
 * @param method get|post|put|delete...
 * @param params like queryString. if a url is index?a=1&b=2, params = {a: '1', b: '2'}
 * @param data post data, use for method put|post
 * @returns {axios}
 */
function ajax(url, method, options) {
    if (options !== undefined) {
        var {params = {}, data = {}} = options
    } else {
        params = data = {}
    }
    return new Promise((resolve, reject) => {
        axios({
            url,
            method,
            params,
            data
        }).then((res) => {
            resolve(res)
        }).catch(error => {
            reject(error)
        })
    })
}